package com.jinniulive.platform.module.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jinniulive.platform.R;
import com.jinniulive.platform.bean.ModifyPersonalInfo;
import com.jinniulive.platform.urls.NetConfig;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class SexSettingActivity extends AppCompatActivity implements View.OnClickListener {

    private RelativeLayout mLayoutBoy,mLayoutGirl;
    private ImageView mImageViewGirl,mImageViewBoy;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sex_setting);
        initView();
        setListener();
    }

    private void setListener() {
        mLayoutGirl.setOnClickListener(this);
        mLayoutBoy.setOnClickListener(this);
        String sex = getIntent().getStringExtra("sex");
        String b="男";
        String g="女";
        if (sex.equals(b)){
            mImageViewBoy.setVisibility(View.VISIBLE);
        }else if (sex.equals(g)){
            mImageViewGirl.setVisibility(View.VISIBLE);
        }

    }

    private void initView() {
        mLayoutBoy = (RelativeLayout) findViewById(R.id.activity_sex_setting_rl_boy);
        mLayoutGirl = (RelativeLayout) findViewById(R.id.activity_sex_setting_rl_girl);
        mImageViewGirl = (ImageView) findViewById(R.id.activity_sex_setting_iv_girl);
        mImageViewBoy = (ImageView) findViewById(R.id.activity_sex_setting_iv_boy);
    }

    public void onClick(View view) {
        if (view != null) {
            switch (view.getId()){
                case R.id.activity_sex_setting_bt_back:
                    finish();
                    break;
                case R.id.activity_sex_setting_rl_boy:
                    mImageViewBoy.setVisibility(View.VISIBLE);
                    mImageViewGirl.setVisibility(View.INVISIBLE);
                    String boy="男";
                    SaveInfo(boy);
                    break;
                case R.id.activity_sex_setting_rl_girl:
                    mImageViewBoy.setVisibility(View.INVISIBLE);
                    mImageViewGirl.setVisibility(View.VISIBLE);
                    String girl="女";
                    SaveInfo(girl);
                    break;
            }
        }
    }
    //保存信息
    private void SaveInfo(String sex) {
        Intent intent = getIntent();
        String tokenID = intent.getStringExtra("tokenID");
        String userID = intent.getStringExtra("userID");
        try {
            String encode = URLEncoder.encode(sex, "UTF-8");
            String format = String.format(NetConfig.XGXB, tokenID, userID, encode);
            RequestParams params =new RequestParams(format);
            x.http().get(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    Gson gson =new Gson();
                    ModifyPersonalInfo info = gson.fromJson(result, ModifyPersonalInfo.class);
                    int errorcode = info.getErrorcode();
                    if (errorcode == 200){
                        Toast.makeText(SexSettingActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
            
            
            
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

       
    }
}
